#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include <unistd.h>

typedef unsigned long long ULL;

bool is_prime(x){
	ULL j = sqrtl(x + 1);
	if(x==2) return true;
	if(x<2 || x%2 == 0) return false;
	for(ULL i = 3;i<=j;i = i+2){
		if(x%i == 0) return false;
	}
	return true;
}

ULL p_count(ULL start,ULL end){
	ULL count = 0;
	for(ULL i = start;i<=end;i++){
		if(is_prime(i)) count++;
	}
	return count;
}

bool is_neg_num_ll(long long x){
	if(x < 0)
		return true;
	else
		return false;
}

int main(int argc,char **argv){
	ULL start_x,end_x;

	if(argc <=1){
		printf("usage : %s [start_num] end_num\n",argv[0]);
		return 1;
	}

	if(argc == 2){
		sscanf(argv[1],"%llu",&end_x);
		if(is_neg_num_ll((long long)end_x)){
			printf("end_num %llu must not neg num!\n",end_x);
			return 2;
		}
		start_x = 0;
	}

	if(argc == 3){
		sscanf(argv[1],"%llu",&start_x);
		sscanf(argv[2],"%llu",&end_x);
		if(is_neg_num_ll((long long)start_x) || is_neg_num_ll((long long)end_x)){
			printf("start_num %llu or end_num %llu must not neg num!\n",\
				start_x,end_x);
			return 3;
		}

		if(start_x >= end_x){
			printf("start_num %llu must < end_num %llu\n",start_x,end_x);
			return 4;
		}
	}

	printf("notice:calc from %llu to %llu\n",start_x,end_x);

	int begin = clock();
	ULL count = p_count(start_x,end_x);
	double stop = ((1.0 * (clock() - begin)) / CLOCKS_PER_SEC) * 1000.0;
	printf("in range (%llu - %llu) p_count is %llu (take %f ms)\n",start_x,end_x,count,stop);

	return 0;
}